package net.wangds.jpa.convertor;

import net.wangds.utils.JsonUtils;
import net.wangds.log.helper.LogHelper;
import net.wangds.web.entrypoint.PagingForm;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.function.Supplier;

public class PagingForm2PageRequestConv implements Converter<PagingForm, PageRequest> {

    private Supplier<Sort> sortFac;

    public PagingForm2PageRequestConv(Supplier<Sort> sortFac){
        this.sortFac = sortFac;
    }

    @Override
    public PageRequest convert(PagingForm source) {
        LogHelper.dev("paging form:"+ JsonUtils.toJsonStr(source));
        return PageRequest.of(source.getPageIndexB1()-1, source.getPageSize(), sortFac.get());
    }
}
